home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / ProcessViewer / ProcessClasses.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  3.8 KB  |  147 lines

  1. unit ProcessClasses;
  2. {*******************************************************************************
  3.   ProcessChecker Demo
  4.   Written by David Clegg, davidclegg@optusnet.com.au.
  5.  
  6.   This unit contains classes used to hold details for monitored and dependant
  7.   processes.
  8. *******************************************************************************}
  9.  
  10. interface
  11.  
  12. uses
  13.   System.Runtime.Serialization, System.Collections;
  14.  
  15. type
  16.   TProcess = class;
  17.  
  18.   /// <summary>
  19.   /// Strongly typed collection of TProcess objects.
  20.   /// </summary>
  21.   [Serializable]
  22.   TProcesses = class(CollectionBase)
  23.   strict private
  24.     function GetProcess(Index: integer): TProcess;
  25.     procedure SetProcess(Index: integer; Value: TProcess);
  26.   public
  27.     procedure Insert(pIndex: integer; pProcess: TProcess);
  28.     function Add(pProcess: TProcess): integer;
  29.     procedure Remove(pProcess: TProcess);
  30.     function Contains(pProcess: TProcess): boolean;
  31.     function IndexOf(pProcess: TProcess): integer;
  32.     property Process[Index: integer]: TProcess read GetProcess write SetProcess; default;
  33.   end;
  34.  
  35.   [Serializable]
  36.     /// <summary>
  37.     /// Base class to contain details of processes to monitor
  38.     /// </summary>
  39.   TProcess = class
  40.   strict private
  41.     FPath: string;
  42.     function GetName: string;
  43.   public
  44.     property Path: string read FPath write FPath;
  45.     property Name: string read GetName;
  46.     constructor Create(const pPath: string);
  47.   end;
  48.  
  49.   /// <summary>
  50.     /// Class to represent a process that is to be watched, and restarted if
  51.     /// necessary.
  52.     /// </summary>
  53.     [Serializable]
  54.   TWatchedProcess = class(TProcess)
  55.   strict private
  56.     FDependantProcesses: TProcesses;
  57.     FCheckResponding: boolean;
  58.   public
  59.     property DependantProcesses: TProcesses read FDependantProcesses;
  60.     property CheckResponding: boolean read FCheckResponding write FCheckResponding;
  61.     constructor Create(const pPath: string);
  62.   end;
  63.  
  64.   /// <summary>
  65.     /// Class to represent a process that is a dependant of a watched process.
  66.     /// If the watched process needs to be restarted, its dependants must be
  67.     /// terminated first.
  68.     /// </summary>
  69.   [Serializable]
  70.   TDependantProcess = class(TProcess)
  71.   strict private
  72.     FParent: TProcess;
  73.   public
  74.     property Parent: TProcess read FParent;
  75.     constructor Create(const pPath: string; pParent: TProcess);
  76.   end;
  77.  
  78. implementation
  79.  
  80. uses
  81.   System.IO;
  82.  
  83. constructor TProcess.Create(const pPath: string);
  84. begin
  85.   inherited Create;
  86.   FPath := pPath;
  87.   // TODO: Add any constructor code here
  88. end;
  89.  
  90. /// <summary>
  91. /// Name of the process to be used for calls to Process.GetProcesses.
  92. /// </summary>
  93. function TProcess.GetName: string;
  94. begin
  95.   Result := System.IO.Path.GetFileNameWithoutExtension(FPath);
  96. end;
  97.  
  98. constructor TWatchedProcess.Create(const pPath: string);
  99. begin
  100.   inherited Create(pPath);
  101.   FDependantProcesses := TProcesses.Create;
  102. end;
  103.  
  104. constructor TDependantProcess.Create(const pPath: string; pParent: TProcess);
  105. begin
  106.   inherited Create(pPath);
  107.   FParent := pParent;
  108. end;
  109.  
  110. function TProcesses.Add(pProcess: TProcess): integer;
  111. begin
  112.   Result := List.Add(pProcess);
  113. end;
  114.  
  115. procedure TProcesses.Insert(pIndex: integer; pProcess: TProcess);
  116. begin
  117.   List.Insert(pIndex, pProcess);
  118. end;
  119.  
  120. procedure TProcesses.Remove(pProcess: TProcess);
  121. begin
  122.   List.Remove(pProcess);
  123. end;
  124.  
  125. function TProcesses.Contains(pProcess: TProcess): boolean;
  126. begin
  127.   Result := List.Contains(pProcess);
  128. end;
  129.  
  130. function TProcesses.IndexOf(pProcess: TProcess): integer;
  131. begin
  132.   Result := List.IndexOf(pProcess);
  133. end;
  134.  
  135. function TProcesses.GetProcess(Index: integer): TProcess;
  136. begin
  137.   Result := List[Index] as TProcess;
  138. end;
  139.  
  140. procedure TProcesses.SetProcess(Index: integer; Value: TProcess);
  141. begin
  142.   List[Index] := Value;
  143. end;
  144.  
  145. end.
  146.  
  147.